from random import Random
from django.core.mail import send_mail
from boxuegu.settings import EMAIL_FROM
from users.models import EmailVerifyRecord
from boxuegu import settings


# 发送邮箱
def send_register_email(email):
    email_record = EmailVerifyRecord()
    code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.save()

    email_title = ''
    email_body = ''

    email_title = '博学谷密码重置链接'
    email_body = '请点击下面的链接重置你的密码：http://127.0.0.1:8000/reset/{0}'.format(code)
    print(email_body)

    send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
    if send_status:
        print('1')


# 生成随机数
def random_str(randomlength=8):
    str = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        str += chars[random.randint(0, length)]
    return str
